package com.hnkywl.sts.service.tjbb;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.hnkywl.sts.dao.tjbb.BzjyjDao;
import com.hnkywl.sts.dao.zjgl.BzjDao;
import com.hnkywl.sts.entity.htgl.Cght;
import com.hnkywl.sts.entity.organ.Qyfr;
import com.hnkywl.sts.entity.zjgl.Bzjyj;
import com.hnkywl.sts.service.sys.YgService;
import com.hnkywl.sts.util.BaseUtils;
import net.ruixin.orm.hibernate.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by HL on 2015/9/20.
 */
@Service
public class BzjyjService {
    private BzjyjDao bzjyjDao;

    @Transactional
    public String findAllBzjyj(String htbm, int pageNo, int pageSize) {
        Page<Bzjyj> page = this.bzjyjDao.findBzjyjList(htbm, pageNo, pageSize);
        List<Bzjyj> list = page.getResult() == null ? new ArrayList<Bzjyj>() : page.getResult();
        String json = BaseUtils.fetchCollectionToJson(list, "id", "cght.id", "cght.htbm", "dqjyzs", "tzjyzs", "pgzsbl", "bzjbl", "bzjje", "wshzje", "dqbzjbl", "zt");
        return "{\"total\":" + page.getTotalCount() + ",\"rows\":[" + json + "]}";
    }



    @Transactional
    public void saveBzjyjByCght(Cght cght) {
        Bzjyj bzjyj = new Bzjyj();
        bzjyj.setCght(cght);
        bzjyj.setDateCreated(new Date());
        bzjyj.setLastUpdated(new Date());
        bzjyj.setCreateUser(YgService.getCurrentYg());
        bzjyj.setCreateOrgan(YgService.getCurrentYg().getZggs());
        bzjyj.setCreateDepartment(YgService.getCurrentYg().getZgjg());
        bzjyjDao.save(bzjyj);
    }

    @Transactional
    public Bzjyj findBzjyjById(Long id) {
        return this.bzjyjDao.get(id);
    }


    @Autowired
    public void setBzjyjDao(BzjyjDao bzjyjDao) {
        this.bzjyjDao = bzjyjDao;
    }
}
